using UnityEngine;
using System.Collections;

public class UtilEx {
	
	public static bool isIOS()
	{
		return Application.platform == RuntimePlatform.IPhonePlayer;
	}
	public static Vector3 cLerp(Vector3 f,Vector3 to,Vector3 center,float t)
	{
		Vector3 fromLocal = f - center;
		Vector3 toLocal = to - center;
		
		
		return Quaternion.Lerp(Quaternion.identity,Quaternion.FromToRotation(fromLocal.normalized,toLocal.normalized),t) 
			* (Mathf.Lerp(fromLocal.magnitude,toLocal.magnitude,t) * fromLocal.normalized) + center;
	}
	// distance can be zero for no checking just facing
	public static bool facing(float DotProductThreshold,Vector3 offenderPos, Vector3 targetPos,Quaternion offenderRotation, float requiredDistance)
	{
		// if 
		if(requiredDistance > 0 && Vector3.Distance(targetPos,offenderPos) > requiredDistance)
			return false;
		
		
		float isFacing = Vector3.Dot((targetPos - offenderPos).normalized,offenderRotation * Vector3.forward);
		if(isFacing < 0 )
			return false;
		
		
		return isFacing > DotProductThreshold;	
	}
	
	public static float facingDifference(Vector3 offenderPos, Vector3 targetPos,Quaternion offenderRotation) {
		float facingAngle = 0.0f;
		
		Quaternion directionNeeded = Quaternion.LookRotation(targetPos - offenderPos);
		facingAngle = Quaternion.Angle(offenderRotation, directionNeeded);
		
		return facingAngle;
	}
}
